package com.tbit.uqbike.service.business.impl;


import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Book;
import com.tbit.uqbike.object.pojo.User;
import com.tbit.uqbike.object.pojo.UserAccountLog;
import com.tbit.uqbike.service.business.BookService;
import com.tbit.uqbike.webmanager.dao.core.BookDao;
import com.tbit.uqbike.webmanager.dao.core.UserAccountLogDao;
import com.tbit.uqbike.webmanager.dao.core.UserDao;
import com.tbit.uqbike.webmanager.dao.log.BookLogDao;
import com.tbit.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("bookService")
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;
    @Autowired
    private BookLogDao bookLogDao;
    @Autowired
    private UserAccountLogDao userAccountLogDao;
    @Autowired
    private UserDao userDao;

    @Override
    public Book getByMachineId(Integer machineId) {
        return bookDao.getByMachineId(machineId);
    }

    @Override
    public List<Book> queryPage(List<Integer> userIds, Integer machineId, String startTime, String endTime, Integer rowCount, Integer pageNO) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userIds", userIds);
        params.put("machineId", machineId);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("start", (pageNO - 1) * rowCount);
        params.put("rowCount", rowCount);

        return bookLogDao.queryPage(params);
    }

    @Override
    public Integer queryCount(List<Integer> userIds, Integer machineId, String startTime, String endTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userIds", userIds);
        params.put("machineId", machineId);
        params.put("startTime", startTime);
        params.put("endTime", endTime);

        return bookLogDao.queryCount(params);
    }

    @Override
    //@Transactional
    public void del(Integer machineId) {
        /**查询记录*/
        Book book = getByMachineId(machineId);
        if (book != null) {
            book.setRideTime(new Date());
            book.setRemark("人工取消预约");
            insertLog(book);

            bookDao.del(machineId);
        }
    }

    @Override
    public void insertLog(Book book) {
        bookLogDao.insert(book);
    }

    @Override
    public void delByPhone(String phone, Integer money) {
        /**查询记录*/
        Book book = getByphone(phone);
        if (book != null) {
            book.setRideTime(new Date());
            book.setMoney(money);
            book.setRemark("manually_cancel_an_appointment");
            /**修改账户金额*/
            if (money > 0F) {
                User user = userDao.getByUserId(book.getUserId());
                if (user != null) {
                    if (user.getMoney() < money) {
                        throw new BaseException("cancal_appoint");
                    }
                    Map<String, Object> map = new HashMap<>();
                    map.put("userId", book.getUserId());
                    map.put("money", -money);
                    map.put("updateTime", DateTimeUtil.getNowTime());
                    userDao.updateMoney(map);

                    /**记录扣费日志*/
                    UserAccountLog userAccountLog = new UserAccountLog();
                    userAccountLog.setAccountId(user.getAccountId());
                    userAccountLog.setMoney(money);
                    userAccountLog.setUserId(book.getUserId());
                    userAccountLog.setOperaTime(new Date());
                    userAccountLog.setRemark("预约车辆扣费");
                    userAccountLog.setType(0);
                    userAccountLogDao.insert(userAccountLog);
                }
            }
            /**记录取消预约日志*/
            insertLog(book);
            Map<String, Object> map = new HashMap<>();
            map.put("phone", phone);
            bookDao.delByPhone(map);
        }

    }

    @Override
    public Book getByphone(String phone) {

        return bookDao.getByphone(phone);
    }
}  